package cn.iocoder.yudao.module.vos.controller.app.edcr;

import cn.hutool.extra.servlet.ServletUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.vos.controller.app.edcr.vo.CdrRetroReqVO;
import cn.iocoder.yudao.module.vos.controller.app.edcr.vo.CdrSearchReqVO;
import cn.iocoder.yudao.module.vos.controller.app.edcr.vo.CdrSearchRespVO;
import cn.iocoder.yudao.module.vos.controller.app.edcr.vo.CdrSearchV2RespVO;
import cn.iocoder.yudao.module.vos.service.ecdr.app.CdrOpenApiService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - cdr话单")
@RestController
@RequestMapping("/data")
@Validated
@RequiredArgsConstructor
public class CdrApiController {

    public static final String APP_SECRET = "appSecret";

    private final CdrOpenApiService openApiService;

    @PostMapping("/v1/search")
    @Operation(summary = "获得cdr话单")
    public CommonResult<CdrSearchRespVO> search(HttpServletRequest request, @RequestBody @Valid CdrSearchReqVO reqVO) {
        String appSecret = request.getHeader(APP_SECRET);
        String ip = ServletUtil.getClientIP(request);
        CdrSearchRespVO respVO = openApiService.searchCdr(appSecret, reqVO, ip);
        return success(respVO);
    }

    @PostMapping("/v2/search")
    @Operation(summary = "获得cdr话单（v2，来源 cdr_ai，含公司名脱敏逻辑）")
    public CommonResult<CdrSearchV2RespVO> searchV2(HttpServletRequest request, @RequestBody @Valid CdrSearchReqVO reqVO) {
        String appSecret = request.getHeader(APP_SECRET);
        String ip = ServletUtil.getClientIP(request);
        CdrSearchV2RespVO respVO = openApiService.searchCdrV2(appSecret, reqVO, ip);
        return success(respVO);
    }

    @PostMapping("/v1/retrospective")
    @Operation(summary = "获得cdr溯源数据（指定截止日期 + 回溯周期）")
    public CommonResult<CdrSearchRespVO> retrospective(HttpServletRequest request, @RequestBody @Valid CdrRetroReqVO reqVO) {
        String appSecret = request.getHeader(APP_SECRET);
        String ip = ServletUtil.getClientIP(request);
        CdrSearchRespVO respVO = openApiService.searchCdrRetrospective(appSecret, reqVO, ip);
        return success(respVO);
    }
}
